<?php

use app\modules\Admin\service\Redis as RedisService;
use Illuminate\Database\Capsule\Manager as DB;

class GoodsController extends CommonController
{
    protected $authExcept = ['lists', 'search', 'detail', 'seckilllistbyactivityid'];

    // 查询商品列表只查代客下单类目
    public function searchAction()
    {
        $params = $this->get();
        $data = goodsModel::getGoodsList($params);
        json(['code' => 1, 'msg' => '商品列表', 'data' => $data]);
    }

    // 查询商品列表(所有类目)
    public function listsAction()
    {
        $params = $this->get();
        $data = goodsModel::getGoodsListAllCate($params);
        json(['code' => 1, 'msg' => '商品列表', 'data' => $data]);
    }

    // 查询商品详情
    public function detailAction()
    {
        $id = $this->get('id');
        $detail = goodsModel::getGoodsDetail($id);

        // 记录足记
        memberviewModel::addView($id);

        json(['code' => 1, 'msg' => '商品详情', 'data' => $detail]);
    }

    public function viewListAction()
    {
        $params = $this->get();
        $data = memberviewModel::getGoodsList($params);
        json(['code' => 1, 'msg' => '商品浏览列表', 'data' => $data]);
    }

    public function viewDeleteAction()
    {
        $params = $this->getPost();
        $result = memberviewModel::del($params);
        json(['code' => 1, 'msg' => '删除成功', 'data' => $result]);
    }

    public function favoriteAction()
    {
        $params = $this->getPost();
        $result = memberfavoriteModel::edit($params);
        json(['code' => 1, 'msg' => $result ? '收藏成功' : '取消收藏', 'data' => $result]);
    }

    public function favoriteListAction()
    {
        $params = $this->get();
        $data = memberfavoriteModel::getGoodsList($params);
        json(['code' => 1, 'msg' => '商品收藏列表', 'data' => $data]);
    }

    public function seckilllistbyactivityidAction()
    {
        $activity_id = $this->get('activity_id', 0);
        $activity = activityModel::query()->find($activity_id);
        if(!$activity){
            json(['code' => 0, 'msg' => '秒杀活动已结束']);
            exit;
        }
        $activity->rules = json_decode($activity->rules);
        $activity_goods_sku_price_list = DB::table('activity_goods_sku_price')
            ->where('activity_id', $activity_id)
            ->where('status', 'up')
            ->get();
        foreach ($activity_goods_sku_price_list as $activity_goods_sku_price_item) {
            $activity_goods_key = "{$activity_id}-{$activity_goods_sku_price_item->goods_id}";
            $activity_goods_list[$activity_goods_key] = [
                'goods_id' => $activity_goods_sku_price_item->goods_id,
                'activity_id' => $activity_goods_sku_price_item->activity_id,
                'sku_price' => [],
                'sku' => [],
            ];
        }
        $activity_goods_list = $activity_goods_list ?? [];
        foreach ($activity_goods_sku_price_list as $activity_goods_sku_price_item) {
            $activity_goods_ids[] = $activity_goods_sku_price_item->goods_id;
            $activity_goods_sku_price_ids[] = $activity_goods_sku_price_item->sku_price_id;
        }
        $activity_goods_ids = array_unique($activity_goods_ids ?? []);
        $goods_sku_price_list = DB::table('goods_sku_price')
            ->whereIn('id', array_unique($activity_goods_sku_price_ids ?? []))
            ->get();
        foreach ($goods_sku_price_list as $goods_sku_price_item) {
            $goods_sku_ids = explode(',', $goods_sku_price_item->goods_sku_ids);
            foreach ($goods_sku_ids as $goods_sku_id) {
                $goods_sku_ids[] = $goods_sku_id;
            }
        }
        $goods_list = DB::table('goods')->whereIn('id', $activity_goods_ids ?? [])->get();
        array_walk($activity_goods_list, function (&$item) use (
            $activity,
            $activity_goods_sku_price_list,
            $goods_list
        ) {
            $item['activity'] = $activity;
            foreach ($goods_list as $goods_item) {
                if ($goods_item->id == $item['goods_id']) {
                    foreach ($goods_item as $goods_key => $goods_value) {
                        $item[$goods_key] = $goods_value;
                    }
                    break;
                }
            }
            foreach ($activity_goods_sku_price_list as $activity_goods_sku_price_item) {
                if ($activity_goods_sku_price_item->goods_id == $item['goods_id']) {
                    $item['price'] = ($item['price'] < $activity_goods_sku_price_item->price)
                        ? $item['price']
                        : $activity_goods_sku_price_item->price;
                    // $item['stock'] += $activity_goods_sku_price_item->stock;
                }
            }
        });

        $total = count($activity_goods_list);
        json(['code' => 1, 'msg' => '秒杀列表', 'data' => [
            'total' => $total,
            'data' => array_values($activity_goods_list),
        ]]);
    }

    public function seckilllistAction()
    {
        $type = $this->get('type', 'ing');
        $page = $this->get('page', 1);

        $per_page = 10;
        $offset = ($page - 1) * $per_page;

        $time = time();
        $temp_activity_list = activityModel::query()
            ->where('status', $type == 'ing' ? 1 : 0)
            ->where('endtime', '>', $time)
            ->orderBy('starttime')
            ->get();
        foreach ($temp_activity_list as $k => $temp_actity_item) {
            $temp_actity_item->rules = json_decode($temp_actity_item->rules);
            $activity_list[] = $temp_actity_item;
            $activity_ids[] = $temp_actity_item->id;
        }
        unset($temp_actity_list);
        $activity_list = $activity_list ?? [];
        $activity_ids = array_unique($activity_ids ?? []);
        $activity_goods_sku_price_list = DB::table('activity_goods_sku_price')
            ->whereIn('activity_id', $activity_ids ?? [])
            ->where('status', 'up')
            ->get();
        foreach ($activity_list as $actity_item) {
            foreach ($activity_goods_sku_price_list as $activity_goods_sku_price_item) {
                if ($activity_goods_sku_price_item->activity_id == $actity_item->id) {
                    $activity_goods_key = "{$activity_goods_sku_price_item->activity_id}-{$activity_goods_sku_price_item->goods_id}";
                    $activity_goods_list[$activity_goods_key] = [
                        'goods_id' => $activity_goods_sku_price_item->goods_id,
                        'activity_id' => $activity_goods_sku_price_item->activity_id,
                        'sku_price' => [],
                        'sku' => [],
                    ];
                }
            }
        }
        $activity_goods_list = $activity_goods_list ?? [];
        foreach ($activity_goods_sku_price_list as $activity_goods_sku_price_item) {
            $activity_goods_ids[] = $activity_goods_sku_price_item->goods_id;
            $activity_goods_sku_price_ids[] = $activity_goods_sku_price_item->sku_price_id;
        }
        $activity_goods_ids = array_unique($activity_goods_ids ?? []);
        $goods_sku_price_list = DB::table('goods_sku_price')
            ->whereIn('id', array_unique($activity_goods_sku_price_ids ?? []))
            ->get();
        foreach ($goods_sku_price_list as $goods_sku_price_item) {
            $goods_sku_ids = explode(',', $goods_sku_price_item->goods_sku_ids);
            foreach ($goods_sku_ids as $goods_sku_id) {
                $goods_sku_ids[] = $goods_sku_id;
            }
        }
        $goods_list = DB::table('goods')->whereIn('id', $activity_goods_ids ?? [])->get();
        array_walk($activity_goods_list, function (&$item) use (
            $activity_list,
            $activity_goods_sku_price_list,
            $goods_list
        ) {
            foreach ($activity_list as $activity_item) {
                if ($activity_item->id == $item['activity_id']) {
                    $item['activity'] = $activity_item;
                    break;
                }
            }
            foreach ($goods_list as $goods_item) {
                if ($goods_item->id == $item['goods_id']) {
                    foreach ($goods_item as $goods_key => $goods_value) {
                        $item[$goods_key] = $goods_value;
                    }
                    break;
                }
            }
            foreach ($activity_goods_sku_price_list as $activity_goods_sku_price_item) {
                if ($activity_goods_sku_price_item->goods_id == $item['goods_id']) {
                    $item['price'] = ($item['price'] < $activity_goods_sku_price_item->price)
                        ? $item['price']
                        : $activity_goods_sku_price_item->price;
                    $item['stock'] += $activity_goods_sku_price_item->stock;
                }
            }
        });

        $total = count($activity_goods_list);
        json(['code' => 1, 'msg' => '秒杀列表', 'data' => [
            'current_page' => $page,
            'last_page' => 1,
            'per_page' => $total,
            'total' => $total,
            'data' => array_values($activity_goods_list),
        ]]);
    }

    public function seckilldetailAction()
    {
        $goods_id = $this->get('goods_id', 0);
        $activity_id = $this->get('activity_id', 0);
        if (empty($goods_id) || empty($activity_id)) {
            throw new Exception('活动参数有误');
        }

        $goods = DB::table('goods')->where('id', $goods_id)->where('status', 'up')->first();
        $activity = activityModel::query()->find($activity_id);
        if (empty($goods) || empty($activity)) {
            throw new Exception('商品或活动不存在');
        }

        $agentGoods = \agentgoodsModel::query()->where('goods_id', $goods['id'])->first();
        if($agentGoods && $agentGoods->status==1 && $agentGoods->qy_status==1){
            if ($agentGoods->qy_self_rules == 1) {
                $qy_rules = $agentGoods->qy_rules ? json_decode($agentGoods->qy_rules, TRUE) : [];
            } else {
                $quanyiRules = \configModel::query()
                    ->where('type', 19)
                    ->where('key',  'quanyi_ratio')
                    ->value('value');
                $qy_rules   = ["ratio"=>$quanyiRules];
            }
            $commission = $this->caculateGoodsCommission($qy_rules, $goods['price'], 1);
            $goods['quanyi_price'] = $commission;
        } else {
            $goods['quanyi_price'] = 0.00;
        }

        // 活动进行中，读取缓存数据
        $time = time();
        if ($activity->status && $time > $activity->starttime && $time < $activity->endtime) {
            $activity_goods_info_key = "activity_{$activity_id}_goods_{$goods_id}";
            $activity_goods_info_val = RedisService::hgetall($activity_goods_info_key);
            $data = json_decode($activity_goods_info_val['info'], true);
            $data['activity'] = json_decode($activity_goods_info_val['activity'],true);
            $data['activity']['rules'] = json_decode($data['activity']['rules'],true);
            $data['activity']['status_code'] = 'ing';
            $data['activity_type'] = 'seckill';

            $data['sku'] = json_decode($activity_goods_info_val['sku'],true);
            $data['sku_price'] = json_decode($activity_goods_info_val['sku_price'],true);
            $data['stock'] = 0;
            $data['sales'] = DB::table('goods')->where('id', $goods_id)->value('sales');

            foreach ($data['sku_price'] as $k => $sku_price) {
                $kucun_key = "activity_{$activity_id}_goods_{$goods_id}_skupriceid_{$sku_price['id']}";
                $data['sku_price'][$k]['stock'] = RedisService::llen($kucun_key);
                $data['stock'] += $data['sku_price'][$k]['stock'];
            }

            json(['code' => 1, 'msg' => '秒杀详情', 'data' => $data]);
            exit;
        }

        $goods->images = explode(',', $goods->images);

        $activity->rules = json_decode($activity->rules);
        if ($activity->status && $activity->starttime < $time && $activity->endtime > $time) {
            $activity->status_code = 'ing';
        } else {
            if (!$activity->status || $activity->starttime > $time) {
                $activity->status_code = 'waiting';
            }
            if ($activity->endtime < $time) {
                $activity->status_code = 'end';
            }
        }
        $goods->activity = $activity;
        $goods->activity_type = 'seckill';

        $activity_goods_sku_price_list = DB::table('activity_goods_sku_price')
            ->where('activity_id', $activity->id)
            ->where('goods_id', $goods->id)
            ->where('status', 'up')
            ->get();
        foreach ($activity_goods_sku_price_list as $activity_goods_sku_price_item) {
            $activity_goods_sku_price_ids[] = $activity_goods_sku_price_item->sku_price_id;
        }
        $activity_goods_sku_price_ids = array_unique($activity_goods_sku_price_ids ?? []);
        $goods_sku_price_list = DB::table('goods_sku_price')
            ->whereIn('id', $activity_goods_sku_price_ids)
            ->where('status', 'up')
            ->get();
        foreach ($goods_sku_price_list as $goods_sku_price_item) {
            $tmp_goods_sku_ids = explode(',', $goods_sku_price_item->goods_sku_ids);
            foreach ($tmp_goods_sku_ids as $tmp_goods_sku_id) {
                $goods_sku_ids[] = $tmp_goods_sku_id;
            }
        }
        $goods_sku_ids = array_unique($goods_sku_ids ?? []);
        $goods_sku_list = DB::table('goods_sku')
            ->where('goods_id', $goods->id)
            ->where(function ($query) use ($goods_sku_ids) {
                $query->where('pid', 0)->orWhereIn('id', $goods_sku_ids);
            })
            ->get();
        $goods->sku_price = [];
        foreach ($activity_goods_sku_price_list as $activity_goods_sku_price_item) {
            foreach ($goods_sku_price_list as $goods_sku_price_item) {
                if ($activity_goods_sku_price_item->sku_price_id == $goods_sku_price_item->id) {
                    $activity_goods_sku_price_item->id = $goods_sku_price_item->id;
                    $activity_goods_sku_price_item->goods_sku_id_arr = explode(',', $goods_sku_price_item->goods_sku_ids);
                    break;
                }
            }
            $goods->sku_price[] = $activity_goods_sku_price_item;
            $goods->price = ($goods->price < $activity_goods_sku_price_item->price)
                ? $goods->price
                : $activity_goods_sku_price_item->price;
            $goods->stock += $activity_goods_sku_price_item->stock;
        }
        $goods->sku = [];
        foreach ($goods_sku_list as $goods_sku_item) {
            if ($goods_sku_item->goods_id == $goods->id && $goods_sku_item->pid == 0) {
                $goods_sku_item->content = [];
                $goods->sku[] = json_decode(json_encode($goods_sku_item), true);
            }
        }
        foreach ($goods->sku as $goods_sku_key => $goods_sku_val) {
            foreach ($goods_sku_list as $goods_sku_item) {
                if ($goods_sku_item->pid == $goods_sku_val['id']) {
                    $goods->sku[$goods_sku_key]['content'][] = $goods_sku_item;
                }
            }
        }

        json(['code' => 1, 'msg' => '秒杀详情', 'data' => $goods]);
    }

    private static function caculateGoodsCommission($commissionRule, $amount = 0, $goodsNum = 1)
    {
        $commission = 0;
        if (isset($commissionRule['ratio']) && $commissionRule['ratio'] > 0) {
            $commission = round($amount * $commissionRule['ratio'] * 0.01, 2);
        }
        if (isset($commissionRule['money']) && $commissionRule['money'] > 0) {
            $commission = $commissionRule['money'] * $goodsNum;
        }
        return number_format($commission, 2, '.', '');
    }

    public function combolistAction()
    {
        $params = $this->get();
        $page = $params['page'] ?? 1;
        $per_page = $params['per_page'] ?? 10;
        $offset = ($page - 1) * $per_page;

        $total = goodsModel::where('is_package', 1)->where('status', 'up')->count();
        $list = goodsModel::where('is_package', 1)
            ->where('status', 'up')
            ->orderBy('id', 'desc')
            ->offset($offset)
            ->limit($per_page)
            ->get();

        json(['code' => 1, 'msg' => '商品收藏列表', 'data' => [
            'current_page' => $page,
            'last_page' => 1,
            'per_page' => $total,
            'total' => $total,
            'data' => $list,
        ]]);
    }

    public function combodetailAction()
    {
        $id = $this->get('id', 0);
        $goods = goodsModel::where('id', $id)->where('status', 'up')->first();
        if (empty($goods)) {
            json(['code' => 0, 'msg' => '套餐不存在或已下架']);
            exit;
        }

        $goods_combo = DB::table('goods_package')->where('goods_id', $id)->first();
        $goods_info = json_decode($goods_combo->relation_goods_info, true) ?: [];
        $goods_ids = array_column($goods_info, 'id');
        $card_info = json_decode($goods_combo->card_info, true) ?: [];
        $card_ids = array_column($card_info, 'id');

        $goods_list = goodsModel::whereIn('id', $goods_ids)->get();
        $goods_sku_price_list = goodsskupriceModel::whereIn('goods_id', $goods_ids)
            ->where('status', 'up')
            ->get();
        foreach ($goods_sku_price_list as $goods_sku_price_item) {
            $tmp_goods_sku_ids = explode(',', $goods_sku_price_item->goods_sku_ids);
            foreach ($tmp_goods_sku_ids as $tmp_goods_sku_id) {
                $goods_sku_ids[] = $tmp_goods_sku_id;
            }
        }
        $goods_sku_ids = array_unique($goods_sku_ids ?? []);
        $goods_sku_list = DB::table('goods_sku')
            ->whereIn('goods_id', $goods_ids)
            ->where(function ($query) use ($goods_sku_ids) {
                $query->where('pid', 0)->orWhereIn('id', $goods_sku_ids);
            })
            ->get();

        array_walk($goods_info, function (&$item) use ($goods_list, $goods_sku_price_list, $goods_sku_list) {
            $item['number'] = (int) $item['number'];
            foreach ($goods_list as $goods_item) {
                if ($goods_item->id == $item['id']) {
                    $item['goods_detail'] = $goods_item;
                    break;
                }
            }

            $sku_price = [];
            foreach ($goods_sku_price_list as $goods_sku_price_item) {
                if ($goods_sku_price_item->goods_id == $goods_item->id) {
                    $goods_sku_price_item->goods_sku_id_arr = explode(',', $goods_sku_price_item->goods_sku_ids);
                    $sku_price[] = $goods_sku_price_item;
                }
            }
            $item['goods_detail']['sku_price'] = $sku_price;

            $sku = [];
            foreach ($goods_sku_list as $goods_sku_item) {
                if ($goods_sku_item->goods_id == $item['id'] && $goods_sku_item->pid == 0) {
                    $goods_sku_item->content = [];
                    $sku[] = $goods_sku_item;
                }
            }
            foreach ($sku as $goods_sku_key => $goods_sku_val) {
                $content = [];
                foreach ($goods_sku_list as $goods_sku_item) {
                    if ($goods_sku_item->pid == $goods_sku_val->id) {
                        $content[] = $goods_sku_item;
                    }
                }
                $goods_sku_val->content = $content;
                $sku[$goods_sku_key] = $goods_sku_val;
            }
            $item['goods_detail']['sku'] = $sku;
        });

        $card_list = DB::table('pickgoods_card')
            ->whereIn('id', $card_ids)
            ->get();
        array_walk($card_info, function (&$item) use ($card_list) {
            foreach ($card_list as $card_item) {
                if ($item['id'] == $card_item->id) {
                    $item['card_detail'] = $card_item;
                    break;
                }
            }
        });

        $data = [
            'id' => $goods->id,
            'title' => $goods->title,
            'subtitle' => $goods->subtitle,
            'price' => $goods->price,
            'image' => $goods->image,
            'images' => $goods->images,
            'content' => $goods->content,
            'goods_info' => $goods_info,
            'card_info' => $card_info,
        ];

        json(['code' => 1, 'msg' => '套餐详情', 'data' => $data]);
    }

    public function combopreorderAction()
    {
        $goods_id = $this->getPost('goods_id', 0);
        $sku_price = $this->getPost('sku_price', []);

        $data = (new \app\service\combo())->previewOrder($goods_id, $sku_price);

        json(['code' => 1, 'msg' => '套餐订单预览', 'data' => $data]);
    }

    public function combocreateorderAction()
    {
        $address_id = $this->getPost('address_id', 0);
        $goods_id = $this->getPost('goods_id', 0);
        $sku_price = $this->getPost('sku_price', []);
        $remark = $this->getPost('remark', '');

        $off_price = $this->getPost('off_price');
        if (empty($off_price)) {
            $off_price = 0;
        }

        $predata = (new \app\service\combo())->previewOrder($goods_id, $sku_price);
        if ($predata['need_address'] && empty($address_id)) {
            throw new Exception('请选择收货地址');
        }

        $order = DB::transaction(function () use ($address_id, $goods_id, $remark, $predata, $off_price) {
            $user = \app\service\auth::getUser();
            $combo_goods = $predata['combo_goods'];
            $combo_goods_sku = $predata['combo_goods_sku'];

            $order = new orderModel();
            $order->order_sn = orderModel::generateSn($user->id);
            $order->member_id = $user->id;
            $order->customer_id = 0;
            $order->type = 'goods';
            $order->activity_type = 0;
            $order->goods_amount = $predata['goods_amount'];
            $order->dispatch_amount = 0;
            $order->total_amount = $predata['total_fee'];
            $order->score_amount = 0;
            $order->total_fee = bcsub($predata['total_fee'], $off_price, 2);
            $order->discount_fee = $off_price;
            $order->score_fee = 0;
            $order->coupon_fee = 0;
            $order->activity_discount_money = 0;
            $order->goods_original_amount = $predata['goods_original_amount'];
            $order->off_price = $off_price;
            $order->pay_fee = $order->total_fee;

            if ($address_id) {
                $address = memberaddressModel::find($address_id);
                $order->phone = $address->phone;
                $order->consignee = $address->consignee;
                $order->province_name = $address->province_name;
                $order->city_name = $address->city_name;
                $order->area_name = $address->area_name;
                $order->address = $address->address;
                $order->province_id = $address->province_id;
                $order->city_id = $address->city_id;
                $order->area_id = $address->area_id;
            }
            $order->status = 0;
            $order->remark = $remark;
            $order->coupons_id = 0;
            $order->platform = getHeader('platform');
            $ext = ['combo' => 1];
            $order->ext = json_encode($ext);
            $order->save();

            // 获取配送数据
            if ($combo_goods->dispatch_type) {
                // TODO 发货设置
                $dispatchData = \dispatchModel::getDispatch($combo_goods->dispatch_type, goodsModel::find($goods_id), [
                    'address' => $address,
                    'goods_num' => $predata['total_num'],
                ]);
            }
            // 配送模板 id
            $current_dispatch_id = $dispatchData['dispatch_id'] ?? 0;
            // 配送费
            $current_dispatch_amount = $dispatchData['dispatch_amount'] ?? 0;

            $orderItem = new orderitemModel();
            $orderItem->member_id = $user->id;
            $orderItem->order_id = $order->id;
            $orderItem->goods_id = $goods_id;
            $orderItem->goods_title = $combo_goods->title;
            $orderItem->goods_type = $combo_goods->type;
            $orderItem->goods_sku_price_id = $combo_goods_sku->id;
            $orderItem->goods_sku_text = $combo_goods_sku->goods_sku_text ? implode(',', $combo_goods_sku->goods_sku_text) : '';
            $orderItem->calc_consume = $combo_goods->calc_consume;
            $orderItem->activity_id = 0;
            $orderItem->activity_type = '';
            $orderItem->goods_image = $combo_goods->image;
            $orderItem->goods_original_price = $combo_goods->original_price;
            $orderItem->cost_price = $order->total_fee;
            $orderItem->discount_fee = $order->discount_fee;
            $orderItem->pay_price = $order->total_fee;
            $orderItem->goods_price = $combo_goods->price;
            $orderItem->goods_num = 1;
            $orderItem->goods_weight = 0;
            $orderItem->dispatch_status = 0;
            $orderItem->dispatch_fee = $current_dispatch_amount;
            $orderItem->dispatch_type = $combo_goods->dispatch_type;
            $orderItem->dispatch_id = $current_dispatch_id;
            $orderItem->store_id = 0;
            $orderItem->aftersale_status = 0;
            $orderItem->comment_status = 0;
            $orderItem->refund_status = 0;

            $combo_goods_list = $predata['goods_list'];
            $combo_card_list = $predata['card_list'];
            $goods_combo = ['goods' => [], 'card' => []];
            foreach ($combo_goods_list as $combo_goods_item) {
                $goods_combo['goods'][] = [
                    'goods_id' => $combo_goods_item['goods_id'],
                    'goods_title' => $combo_goods_item['goods_title'],
                    'goods_image' => $combo_goods_item['goods_image'],
                    'goods_price' => $combo_goods_item['goods_price'],
                    'goods_num' => $combo_goods_item['goods_num'],
                    'dispatch_type' => $combo_goods->dispatch_type,
                    'dispatch_status' => 0,
                    'goods_sku_price_id' => $combo_goods_item['goods_sku_price_id'],
                    'goods_sku_text' => $combo_goods_item['goods_sku_price_text'],
                    'refund_status' => 0,
                    'aftersale_status' => 0,
                ];
            }
            foreach ($combo_card_list as $combo_card_item) {
                $goods_combo['card'][] = [
                    'card_id' => $combo_card_item['card_id'],
                    'card_num' => $combo_card_item['card_num'],
                ];
            }
            if (empty($goods_combo['goods']) && empty($goods_combo['card'])) {
                throw new Exception('订单商品异常');
            }
            $orderItem->goods_combo = json_encode($goods_combo);

            $orderItem->save();

            // 订单提交成功，
            \Hook::emit('order_create_after', ['from'  => 'goods', 'orderId' => $order->id]);

            return $order;
        });

        json(['code' => 1, 'msg' => '套餐订单提交', 'data' => $order]);
    }
}
